<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/dxl/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/dxl/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/dxl/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/dxl/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/dxl/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/dxl/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/dxl/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/dxl/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/dxl/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="爬虫,">










<meta name="description" content="一、城市气候与海洋的关系研究1.1导入包12345678910import numpy as npimport pandas as pdfrom pandas import Series,DataFrameimport matplotlib.pyplot as pltfrom pylab import mplmpl.rcParams[&amp;apos;font.sans-serif&amp;apos;] = [">
<meta name="keywords" content="爬虫">
<meta property="og:type" content="article">
<meta property="og:title" content="案例分析">
<meta property="og:url" content="http://yoursite.com/2019/09/20/【数据分析04】案例分析/index.html">
<meta property="og:site_name" content="我的快乐时光">
<meta property="og:description" content="一、城市气候与海洋的关系研究1.1导入包12345678910import numpy as npimport pandas as pdfrom pandas import Series,DataFrameimport matplotlib.pyplot as pltfrom pylab import mplmpl.rcParams[&amp;apos;font.sans-serif&amp;apos;] = [">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-20T12:34:08.838Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="案例分析">
<meta name="twitter:description" content="一、城市气候与海洋的关系研究1.1导入包12345678910import numpy as npimport pandas as pdfrom pandas import Series,DataFrameimport matplotlib.pyplot as pltfrom pylab import mplmpl.rcParams[&amp;apos;font.sans-serif&amp;apos;] = [">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/dxl/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/20/【数据分析04】案例分析/">





  <title>案例分析 | 我的快乐时光</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/dxl/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的快乐时光</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/dxl/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/dxl/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/dxl/2019/09/20/【数据分析04】案例分析/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content>
      <meta itemprop="description" content>
      <meta itemprop="image" content="/dxl/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的快乐时光">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">案例分析</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-20T20:44:46+08:00">
                2019-09-20
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/dxl/categories/爬虫/" itemprop="url" rel="index">
                    <span itemprop="name">爬虫</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/dxl/2019/09/20/【数据分析04】案例分析/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/dxl/2019/09/20/【数据分析04】案例分析/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、城市气候与海洋的关系研究"><a href="#一、城市气候与海洋的关系研究" class="headerlink" title="一、城市气候与海洋的关系研究"></a>一、城市气候与海洋的关系研究</h2><h2 id="1-1导入包"><a href="#1-1导入包" class="headerlink" title="1.1导入包"></a>1.1导入包</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import numpy as np</span><br><span class="line">import pandas as pd</span><br><span class="line">from pandas import Series,DataFrame</span><br><span class="line"></span><br><span class="line">import matplotlib.pyplot as plt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">from pylab import mpl</span><br><span class="line">mpl.rcParams[&apos;font.sans-serif&apos;] = [&apos;FangSong&apos;] # 指定默认字体</span><br><span class="line">mpl.rcParams[&apos;axes.unicode_minus&apos;] = False # 解决保存图像是负号&apos;-&apos;显示为方块的问题</span><br></pre></td></tr></table></figure>

<h3 id="1-2导入各个城市的数据"><a href="#1-2导入各个城市的数据" class="headerlink" title="1.2导入各个城市的数据"></a>1.2导入各个城市的数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">ferrara1 = pd.read_csv(<span class="string">'./ferrara_150715.csv'</span>)</span><br><span class="line">ferrara2 = pd.read_csv(<span class="string">'./ferrara_250715.csv'</span>)</span><br><span class="line">ferrara3 = pd.read_csv(<span class="string">'./ferrara_270615.csv'</span>)</span><br><span class="line">ferrara=pd.concat([ferrara1,ferrara1,ferrara1],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">torino1 = pd.read_csv(<span class="string">'./torino_150715.csv'</span>)</span><br><span class="line">torino2 = pd.read_csv(<span class="string">'./torino_250715.csv'</span>)</span><br><span class="line">torino3 = pd.read_csv(<span class="string">'./torino_270615.csv'</span>)</span><br><span class="line">torino = pd.concat([torino1,torino2,torino3],ignore_index=<span class="literal">True</span>) </span><br><span class="line"></span><br><span class="line">mantova1 = pd.read_csv(<span class="string">'./mantova_150715.csv'</span>)</span><br><span class="line">mantova2 = pd.read_csv(<span class="string">'./mantova_250715.csv'</span>)</span><br><span class="line">mantova3 = pd.read_csv(<span class="string">'./mantova_270615.csv'</span>)</span><br><span class="line">mantova = pd.concat([mantova1,mantova2,mantova3],ignore_index=<span class="literal">True</span>) </span><br><span class="line"></span><br><span class="line">milano1 = pd.read_csv(<span class="string">'./milano_150715.csv'</span>)</span><br><span class="line">milano2 = pd.read_csv(<span class="string">'./milano_250715.csv'</span>)</span><br><span class="line">milano3 = pd.read_csv(<span class="string">'./milano_270615.csv'</span>)</span><br><span class="line">milano = pd.concat([milano1,milano2,milano3],ignore_index=<span class="literal">True</span>) </span><br><span class="line"></span><br><span class="line">ravenna1 = pd.read_csv(<span class="string">'./ravenna_150715.csv'</span>)</span><br><span class="line">ravenna2 = pd.read_csv(<span class="string">'./ravenna_250715.csv'</span>)</span><br><span class="line">ravenna3 = pd.read_csv(<span class="string">'./ravenna_270615.csv'</span>)</span><br><span class="line">ravenna = pd.concat([ravenna1,ravenna2,ravenna3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">asti1 = pd.read_csv(<span class="string">'./asti_150715.csv'</span>)</span><br><span class="line">asti2 = pd.read_csv(<span class="string">'./asti_250715.csv'</span>)</span><br><span class="line">asti3 = pd.read_csv(<span class="string">'./asti_270615.csv'</span>)</span><br><span class="line">asti = pd.concat([asti1,asti2,asti3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">bologna1 = pd.read_csv(<span class="string">'./bologna_150715.csv'</span>)</span><br><span class="line">bologna2 = pd.read_csv(<span class="string">'./bologna_250715.csv'</span>)</span><br><span class="line">bologna3 = pd.read_csv(<span class="string">'./bologna_270615.csv'</span>)</span><br><span class="line">bologna = pd.concat([bologna1,bologna2,bologna3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">piacenza1 = pd.read_csv(<span class="string">'./piacenza_150715.csv'</span>)</span><br><span class="line">piacenza2 = pd.read_csv(<span class="string">'./piacenza_250715.csv'</span>)</span><br><span class="line">piacenza3 = pd.read_csv(<span class="string">'./piacenza_270615.csv'</span>)</span><br><span class="line">piacenza = pd.concat([piacenza1,piacenza2,piacenza3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">cesena1 = pd.read_csv(<span class="string">'./cesena_150715.csv'</span>)</span><br><span class="line">cesena2 = pd.read_csv(<span class="string">'./cesena_250715.csv'</span>)</span><br><span class="line">cesena3 = pd.read_csv(<span class="string">'./cesena_270615.csv'</span>)</span><br><span class="line">cesena = pd.concat([cesena1,cesena2,cesena3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">faenza1 = pd.read_csv(<span class="string">'./faenza_150715.csv'</span>)</span><br><span class="line">faenza2 = pd.read_csv(<span class="string">'./faenza_250715.csv'</span>)</span><br><span class="line">faenza3 = pd.read_csv(<span class="string">'./faenza_270615.csv'</span>)</span><br><span class="line">faenza = pd.concat([faenza1,faenza2,faenza3],ignore_index=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h3 id="1-3去除没用的列"><a href="#1-3去除没用的列" class="headerlink" title="1.3去除没用的列"></a>1.3去除没用的列</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">city_list = [ferrara,torino,mantova,milano,ravenna,asti,bologna,piacenza,cesena,faenza]</span><br><span class="line"><span class="keyword">for</span> city <span class="keyword">in</span> city_list:</span><br><span class="line">    city.drop(labels=<span class="string">'Unnamed: 0'</span>,axis=<span class="number">1</span>,inplace=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h3 id="1-4制造特征数据与距离"><a href="#1-4制造特征数据与距离" class="headerlink" title="1.4制造特征数据与距离"></a>1.4制造特征数据与距离</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">city_max_temp = []</span><br><span class="line">city_dist = []</span><br><span class="line"><span class="keyword">for</span> city <span class="keyword">in</span> city_list:</span><br><span class="line">    temp_max = city[<span class="string">"temp"</span>].max()</span><br><span class="line">    dist_max = city[<span class="string">"dist"</span>].max()</span><br><span class="line">    city_max_temp.append(temp_max)</span><br><span class="line">    city_dist.append(dist_max)</span><br><span class="line">feature =np.array(city_dist)</span><br><span class="line">target =np.array(city_max_temp)</span><br></pre></td></tr></table></figure>

<h3 id="1-5训练模型"><a href="#1-5训练模型" class="headerlink" title="1.5训练模型"></a>1.5训练模型</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.linear_model <span class="keyword">import</span> LinearRegression</span><br><span class="line">linner = LinearRegression()</span><br><span class="line">linner.fit(feature.reshape((<span class="number">-1</span>,<span class="number">1</span>)),target)</span><br><span class="line"><span class="comment">#线性回归方程：特征指数二维矩阵</span></span><br></pre></td></tr></table></figure>

<h3 id="1-7预测"><a href="#1-7预测" class="headerlink" title="1.7预测"></a>1.7预测</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">linner.predict([[<span class="number">175</span>],[<span class="number">201</span>]])</span><br></pre></td></tr></table></figure>

<h3 id="1-8绘图"><a href="#1-8绘图" class="headerlink" title="1.8绘图"></a>1.8绘图</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 产生点</span></span><br><span class="line">x = np.linspace(<span class="number">0</span>,<span class="number">360</span>,num=<span class="number">100</span>)</span><br><span class="line">y = linner.predict(x.reshape(<span class="number">-1</span>,<span class="number">1</span>))</span><br><span class="line"><span class="comment">#绘图</span></span><br><span class="line">plt.scatter(city_dist,city_max_temp)</span><br><span class="line">plt.scatter(x,y)</span><br><span class="line">plt.xlabel(<span class="string">'距离'</span>)</span><br><span class="line">plt.ylabel(<span class="string">'最高温度'</span>)</span><br><span class="line">plt.title(<span class="string">'距离和最高温度之间的关系'</span>)</span><br></pre></td></tr></table></figure>

<h2 id="二、电影分类"><a href="#二、电影分类" class="headerlink" title="二、电影分类"></a>二、电影分类</h2><h3 id="1、导包"><a href="#1、导包" class="headerlink" title="1、导包"></a>1、导包</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br></pre></td></tr></table></figure>

<h3 id="2、数据读取"><a href="#2、数据读取" class="headerlink" title="2、数据读取"></a>2、数据读取</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">data = pd.read_excel(<span class="string">"./my_films.xlsx"</span>)</span><br><span class="line">data.head(<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3、特征指数、目标对象"><a href="#3、特征指数、目标对象" class="headerlink" title="3、特征指数、目标对象"></a>3、特征指数、目标对象</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">feature = data[[&apos;Action lens&apos;,&apos;Love lens&apos;]]</span><br><span class="line">target = data[&quot;target&quot;]</span><br></pre></td></tr></table></figure>

<h3 id="4、模型训练及预测"><a href="#4、模型训练及预测" class="headerlink" title="4、模型训练及预测"></a>4、模型训练及预测</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.neighbors <span class="keyword">import</span> KNeighborsClassifier</span><br><span class="line"><span class="comment"># 模型训练</span></span><br><span class="line">knn = KNeighborsClassifier(n_neighbors=<span class="number">5</span>)</span><br><span class="line">knn.fit(feature,target)</span><br><span class="line"><span class="comment"># 预测</span></span><br><span class="line">knn.predict([[<span class="number">19</span>,<span class="number">19</span>]])</span><br><span class="line"><span class="comment">#模型得分确定n_neighbors=5</span></span><br><span class="line">knn.score(feature,target)</span><br></pre></td></tr></table></figure>

<h2 id="三、预测年收入是否大于50k美元"><a href="#三、预测年收入是否大于50k美元" class="headerlink" title="三、预测年收入是否大于50k美元"></a>三、预测年收入是否大于50k美元</h2><p>1、读取adult.txt文件，最后一列是年收入，并使用KNN算法训练模型，然后使用模型预测一个人的年收入是否大于50</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">df = pd.read_csv(<span class="string">"./data/adults.txt"</span>)</span><br><span class="line">df.head(<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<p>2、获取年龄、教育程度、职位、每周工作时间作为机器学习数据，获取薪水作为对应结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#样本数据的提取</span></span><br><span class="line">feature = df[[<span class="string">'age'</span>,<span class="string">'education_num'</span>,<span class="string">'occupation'</span>,<span class="string">'hours_per_week'</span>]]</span><br><span class="line">target = df[<span class="string">'salary'</span>]</span><br></pre></td></tr></table></figure>

<p>3、数据转换</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">s = feature[<span class="string">'occupation'</span>].unique()</span><br><span class="line">dic = &#123;&#125;</span><br><span class="line">j = <span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> s:</span><br><span class="line">    dic[i] = j</span><br><span class="line">    j += <span class="number">1</span></span><br><span class="line">feature[<span class="string">'occupation'</span>] = feature[<span class="string">'occupation'</span>].map(dic)</span><br></pre></td></tr></table></figure>

<p>4、样本数据的拆分</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#样本数据的拆分  32560</span></span><br><span class="line">x_train = feature[:<span class="number">32500</span>]</span><br><span class="line">y_train = target[:<span class="number">32500</span>]</span><br><span class="line"><span class="comment">#测试数据</span></span><br><span class="line">x_test = feature[<span class="number">32500</span>:]</span><br><span class="line">y_test = target[<span class="number">32500</span>:]</span><br></pre></td></tr></table></figure>

<p>5、模型训练及预测</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">knn = KNeighborsClassifier(n_neighbors=<span class="number">50</span>)</span><br><span class="line">knn.fit(x_train,y_train)</span><br><span class="line">knn.score(x_test,y_test)</span><br></pre></td></tr></table></figure>

<h2 id="四、手写数字识别"><a href="#四、手写数字识别" class="headerlink" title="四、手写数字识别"></a>四、手写数字识别</h2><p>1、导入包</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="comment"># bmp 图片后缀</span></span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line">%matplotlib inline</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> sklearn.neighbors <span class="keyword">import</span> KNeighborsClassifier</span><br></pre></td></tr></table></figure>

<p>2、提炼样本数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">feature = []</span><br><span class="line">target = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">0</span>,<span class="number">10</span>):</span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">501</span>):</span><br><span class="line">        img_path = <span class="string">'./data/'</span>+str(i)+<span class="string">'/'</span>+str(i)+<span class="string">'_'</span>+str(j)+<span class="string">'.bmp'</span></span><br><span class="line">        img_arr = plt.imread(img_path)</span><br><span class="line">        feature.append(img_arr)</span><br><span class="line">        target.append(i)</span><br><span class="line">feature = np.array(feature)</span><br><span class="line">target = np.array(target)</span><br></pre></td></tr></table></figure>

<p>3、数据的降维</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">feature = feature.reshape(<span class="number">5000</span>,<span class="number">784</span>)</span><br><span class="line">feature.shape</span><br></pre></td></tr></table></figure>

<p>4、将样本数据打乱</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">np.random.seed(<span class="number">3</span>)</span><br><span class="line">np.random.shuffle(feature)</span><br><span class="line">np.random.seed(<span class="number">3</span>)</span><br><span class="line">np.random.shuffle(target)</span><br></pre></td></tr></table></figure>

<p>5、数据的拆分</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">x_train = feature[:<span class="number">4950</span>]</span><br><span class="line">y_train = target[:<span class="number">4950</span>]</span><br><span class="line"></span><br><span class="line">x_test = feature[<span class="number">4950</span>:]</span><br><span class="line">y_test = target[<span class="number">4950</span>:]</span><br></pre></td></tr></table></figure>

<p>6、实例化模型对象、训练</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">knn = KNeighborsClassifier(n_neighbors=<span class="number">15</span>)</span><br><span class="line">knn.fit(x_train,y_train)</span><br><span class="line">knn.score(x_test,y_test)</span><br></pre></td></tr></table></figure>

<p>7、保存训练模型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">knn = joblib.load(<span class="string">'./digist_knn.m'</span>)</span><br></pre></td></tr></table></figure>

<p>8、图片识别</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">pic_path =<span class="string">"./5.png"</span></span><br><span class="line"></span><br><span class="line">immr = plt.imread(pic_path)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">pic</span><span class="params">(immr,knn)</span>:</span></span><br><span class="line">    immr=immr.mean(axis=<span class="number">2</span>)</span><br><span class="line">    a =immr.shape[<span class="number">0</span>]</span><br><span class="line">    b=immr.shape[<span class="number">1</span>]</span><br><span class="line">    <span class="keyword">import</span> scipy.ndimage <span class="keyword">as</span> ndimage</span><br><span class="line">    pic = ndimage.zoom(immr,zoom = (<span class="number">28</span>/a,<span class="number">28</span>/b))</span><br><span class="line">    pic = pic.reshape((<span class="number">1</span>,<span class="number">784</span>))</span><br><span class="line">    a =picture(knn,pic)</span><br><span class="line">    <span class="keyword">return</span> a</span><br><span class="line">a =pic(immr,knn)</span><br><span class="line">a</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://yoursite.com/2019/09/20/【数据分析04】案例分析/" title="案例分析">http://yoursite.com/2019/09/20/【数据分析04】案例分析/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/dxl/tags/爬虫/" rel="tag"># 爬虫</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/dxl/2019/09/20/【数据分析03】数据分析三/" rel="next" title="数据分析Matplotlib">
                <i class="fa fa-chevron-left"></i> 数据分析Matplotlib
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/dxl/2019/09/20/【爬虫】常见的报错 - 副本/" rel="prev" title>
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/dxl/images/avatar.png" alt>
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/dxl/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/dxl/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/dxl/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-sign-out"></i>
                我的友链
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="tencent://message/?Menu=yes&uin=1258517737&Site=QQ%E6%9E%81%E5%AE%A2&Service=300&sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45/" title="申请坑位" target="_blank">申请坑位</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、城市气候与海洋的关系研究"><span class="nav-number">1.</span> <span class="nav-text">一、城市气候与海洋的关系研究</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1导入包"><span class="nav-number">2.</span> <span class="nav-text">1.1导入包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2导入各个城市的数据"><span class="nav-number">2.1.</span> <span class="nav-text">1.2导入各个城市的数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3去除没用的列"><span class="nav-number">2.2.</span> <span class="nav-text">1.3去除没用的列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4制造特征数据与距离"><span class="nav-number">2.3.</span> <span class="nav-text">1.4制造特征数据与距离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5训练模型"><span class="nav-number">2.4.</span> <span class="nav-text">1.5训练模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7预测"><span class="nav-number">2.5.</span> <span class="nav-text">1.7预测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-8绘图"><span class="nav-number">2.6.</span> <span class="nav-text">1.8绘图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、电影分类"><span class="nav-number">3.</span> <span class="nav-text">二、电影分类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、导包"><span class="nav-number">3.1.</span> <span class="nav-text">1、导包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、数据读取"><span class="nav-number">3.2.</span> <span class="nav-text">2、数据读取</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、特征指数、目标对象"><span class="nav-number">3.3.</span> <span class="nav-text">3、特征指数、目标对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、模型训练及预测"><span class="nav-number">3.4.</span> <span class="nav-text">4、模型训练及预测</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、预测年收入是否大于50k美元"><span class="nav-number">4.</span> <span class="nav-text">三、预测年收入是否大于50k美元</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、手写数字识别"><span class="nav-number">5.</span> <span class="nav-text">四、手写数字识别</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-hand-peace-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/dxl/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/dxl/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/dxl/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/dxl/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/dxl/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/dxl/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '13B0JGDuA6ttduN8AQaR8CzF-gzGzoHsz',
        appKey: 'I13r9r5mVgq4jQYpYy6V4gW3',
        placeholder: '欢迎大佬指点~~~',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
